package com.frxuexi.demo01_compose.view

import android.text.TextUtils
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Lock
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.ToastUtils

@Composable
fun myLogin(navController: NavHostController) {
    var value by remember {
        mutableStateOf("admin6")
    }
    var pass by remember {
        mutableStateOf("666")
    }

    Column {
        TextButton(onClick = { /*TODO*/ }) {
            Icon(imageVector = Icons.Default.ArrowBack, contentDescription = "")
        }
        Column(
            modifier = Modifier
                .fillMaxWidth()
                .fillMaxHeight()
                .padding(horizontal = 40.dp),
            horizontalAlignment = Alignment.CenterHorizontally,

            ) {
            Spacer(modifier = Modifier.height(100.dp))
            Text(text = "D资讯", fontSize = 18.sp, fontWeight = FontWeight.Bold)
            Spacer(modifier = Modifier.height(10.dp))
            Text(text = "文本")
            Spacer(modifier = Modifier.height(50.dp))
            OutlinedTextField(modifier = Modifier
                .fillMaxWidth(),
                leadingIcon = {
                Icon(imageVector = Icons.Default.AccountCircle, contentDescription = "")
            },
                value = value,
                label = { Text(text = "用户名") },
                onValueChange = {
                    value = it
                })
            Spacer(modifier = Modifier.height(20.dp))
            OutlinedTextField(modifier = Modifier
                .fillMaxWidth(), leadingIcon = {
                Icon(imageVector = Icons.Default.Lock, contentDescription = "")
            }, value = pass,
                label = { Text(text = "密码") },
                onValueChange = { pass = it },
                visualTransformation = PasswordVisualTransformation(),
                trailingIcon={
                    Button(onClick = { /*TODO*/ }) {
                        Text(text = "验证码")
                    }
                }
            )
            Spacer(modifier = Modifier.height(40.dp))
            Button(modifier = Modifier
                .fillMaxWidth()
                .height(50.dp)
                ,
                shape = RoundedCornerShape(10.dp),
                colors= ButtonDefaults.buttonColors(containerColor= Color.Blue),
                onClick = {
                    login(value,pass,navController)

                }) {
                Text(text = "登录")
            }

            TextButton(onClick = { }) {
                Text(text = "忘记密码")
            }

        }
    }
    /*Row(modifier = Modifier
        .fillMaxWidth().fillMaxHeight(),
        horizontalArrangement = Arrangement.Center,
        verticalAlignment = Alignment.CenterVertically) {
        Text(text = "文本", color = Color.Red,fontSize=18.sp, fontWeight = FontWeight.Bold)
        Divider(thickness=2.dp, modifier = Modifier.width(2.dp).height(20.dp))
        Text(text = "文本")
        Text(text = "文本")
    }*/
}

fun login(username:String,password:String,navController: NavHostController) {

    val name = "admin6"
    val pass = "666"
    if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
        ToastUtils.showLong("账号和密码都不能为空")
        return
    }

    /*if(!username.matches(Regex("1[0-9]{10}"))){
        ToastUtils.showLong("不是合法手机号")
        return
    }*/

    /*if(!username.matches(Regex("[0-9A-z_]{6,12}@[0-9a-z]{2,5}.com"))){
        ToastUtils.showLong("不是合法邮箱")
        return
    }*/
    if(!RegexUtils.isUsername(username)){
        ToastUtils.showLong("不是合法用户名")
        return
    }

    if(username==name && password==pass){
        ToastUtils.showLong("登录成功")
        //跳转
        navController.navigate("main/$username?pass=$pass")

    }else{
        ToastUtils.showLong("用户名或者密码错误")
    }
}